function [TSnet] = createTumorSuppressorsNet(dasURL,dsn,display)
% Cretes a protein interaction network involving tumor suppressors for the specified DAS molecular interaction database
%
% createTumorSupressorsNet() constructs a protein-protein interaction
% network that consists of interactions among tumor supressor (TS) proteins and
% their interacting partners.
%
% SYNTAX: [TSnet] = createTumorSuppressorsNet(dasURL,dsn,display)
%          dasUrl - URL for the DAS service. 
%          dsn - database providing ptn-ptn interaction
%          display - true/false, to display or not the biograph object
%
% See also tumorSupressorNetwork_Fig5AB(),
% createTSConnectionHeatmap_Fig5CD()
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, Jan 2009.

% Tumor supressor genes using official symbols
global tsGenes;
tsGenes = {'RB1', 'TP53', 'CDKN2A' 'APC', 'BRCA1', 'BRCA2', 'WT1', 'NF1', ...
    'NF2', 'VHL', 'MEN1', 'PTCH1', 'PTEN', 'SMAD4', 'CDH1', 'STK11', 'SMARCB1', ...
    'EXT1', 'EXT2', 'TSC1', 'TSC2', 'MSH2'};

% Tumor supressor genes using Entrez IDs
global tsEntrezID;
tsEntrezID = {'5925', '7157', '1029', '324', '672', '675', '7490', '4763', ...
    '4771', '7428', '4221', '5727', '5728', '4089', '999', '6794', '6598', ...
    '608177', '2132', '7248', '7249', '4436'};

global tsInt;
tsInt = struct('ts',{},'EntrezID',{},'interactors', ...
    struct('symbol', {}, 'EntrezID', {}, 'location', {}) );

for i=1:size(tsEntrezID,2)
   
    %eg. http://dasmi.bioinf.mpi-inf.mpg.de/das/hprdEntrez/interaction?interactor=5925
    %eg. http://dasmi.bioinf.mpi-inf.mpg.de/das/ddi/interaction?interactor=5925
    [xmlS] = executeDASCommand(dasURL,'command', 'interaction', 'DSN', dsn,'segment',tsEntrezID{i});
    
    tsInt(i).ts = tsGenes{i};
    tsInt(i).EntrezID = tsEntrezID{i};
    
    if (isfield(xmlS,'INTERACTOR'))
        
        for j=1:size(xmlS.INTERACTOR,2)
            tsInt(i).interactors(j).EntrezID = xmlS.INTERACTOR{j}.ATTRIBUTE.dbAccessionId;
            tsInt(i).interactors(j).symbol = xmlS.INTERACTOR{j}.ATTRIBUTE.shortLabel;
            tsInt(i).interactors(j).location = '';
            
            for k=1:size(xmlS.INTERACTOR{j}.DETAIL,2)
                
                if (strcmp(xmlS.INTERACTOR{j}.DETAIL{k}.ATTRIBUTE.property,'location'))
                    tsInt(i).interactors(j).location = xmlS.INTERACTOR{j}.DETAIL{k}.ATTRIBUTE.value; %chrm location
                end
            end
        end

    end
end

graphLabels = tsEntrezID; %start with ts ids
for i=1:size(tsInt,2)
    if (size(tsInt(i).interactors,2) > 0)
        graphLabels = [graphLabels tsInt(i).interactors(1:end).EntrezID];
    end
end
graphLabels = unique(graphLabels);

%Assemble connectivity matrix
cm = zeros(size(graphLabels,2),size(graphLabels,2));
for i=1:size(tsInt,2)
    
    idxTS = strmatch(tsInt(i).EntrezID, graphLabels, 'exact');
    
    for j=1:size(tsInt(i).interactors,2)
        idxInt = strmatch(tsInt(i).interactors(j).EntrezID, graphLabels, 'exact');
        cm(idxTS,idxInt) = 1;
        %cm(idxInt,idxTS) = 1;
    end
    
end

%Create network
TSnet = biograph(cm,graphLabels,'ShowArrows', 'off', 'LayoutType', 'hierarchical');


%Customize nodes in the graph
node_ids = get(TSnet,'Nodes');
for i=1:size(graphLabels,2)
    
    idx = strmatch(graphLabels{i},tsEntrezID,'exact');
    if(~isempty(idx)) %TS gene
        set(node_ids(i),'Color',[1 0 0]);
        set(node_ids(i),'LineColor',[1.0000 0.7490 1.0000]);
        set(node_ids(i),'LineWidth',1);
        set(node_ids(i),'Label',tsGenes{idx});
        
    else %other genes
        set(node_ids(i),'Color',[0 0.6824 0.7529]);
        set(node_ids(i),'LineColor',[0 0.8078 0.7529]);
        set(node_ids(i),'LineWidth',1);
        set(node_ids(i),'Shape','ellipse');
        set(node_ids(i),'Label',findLabel(TSnet.Nodes(i).ID));
        
    end
end

dolayout(TSnet);
if (display)
    view(TSnet);
end



function [label] = findLabel(nodeID)
global tsInt;
label = '';
for i=1:size(tsInt,2)
   
    if (size(tsInt(i).interactors,2) > 0)
        ids = {tsInt(i).interactors(1:end).EntrezID};
        idx = find(strcmp(nodeID, ids), 1);

        if (~isempty(idx))
            intLabels = {tsInt(i).interactors(1:end).symbol};
            label = intLabels{idx};
            break;
        end
    end
    
end